<?php
// contact to database
$connect = mysql_connect("localhost", "root", "") or die ("Error , check your server connection.");
mysql_select_db("stms");
 
//Get data in local variable

$SubjectCode=$_POST['SubjectCode'];
$Subject=$_POST['Subject'];
$Faculty=$_POST['Faculty'];
$Department=$_POST['Department'];
$Level=$_POST['Level'];
$Credit=$_POST['Credit'];




$result=mysql_query("SELECT * FROM course WHERE SubjectCode='$SubjectCode'");


 $insert="Add";
 $update="Update";
 $delete="Delete";
 
 if($_POST['Add']==$insert)
 {

 if (mysql_num_rows($result) == 1){
 	  	 header("Location:http://localhost/stms/pages/courseadmin.php?err=This SubjectCode is already exist");

// echo "This Regno is already exist";
 }
 else
 {
 
// check for null values
	if ($SubjectCode==""){
		  	 header("Location:http://localhost/stms/pages/courseadmin.php?err=SubjectCode must be entered, hit back button and re-enter information");

	//echo "All fields must be entered, hit back button and re-enter information";
	}
	else{
	
	$query="insert into course values('$SubjectCode','$Subject','$Faculty','$Department','$Level','$Credit')";
	
	//echo mysql_num_rows($query);
	mysql_query($query)  or die(mysql_error());
		  	 header("Location:http://localhost/stms/pages/courseadmin.php?err=Your message has been received");

	//echo "Your message has been received";
	}
	}
}
else if($_POST['Update']==$update)
{
if (mysql_num_rows($result) == 1)
{
	$upd="UPDATE course SET Subject='$Subject',Faculty='$Faculty',Department='$Department',Level='$Level',Credit='$Credit' WHERE SubjectCode='$SubjectCode'";

	mysql_query($upd)  or die(mysql_error());
		  	 header("Location:http://localhost/stms/pages/courseadmin.php?err=Updated successfully");

	//echo "Updated successfully";
 }
 
 else 
 	  	 header("Location:http://localhost/stms/pages/courseadmin.php?err=There is no SubjectCode as you given");

 //echo "There is no Regno as you given";
 }
 else if($_POST['Delete']==$delete)
 {
 
if (mysql_num_rows($result) == 1)
 {
 $del="DELETE FROM course WHERE SubjectCode='$SubjectCode'";
 mysql_query($del)  or die(mysql_error());
 	  	 header("Location:http://localhost/stms/pages/courseadmin.php?err=Deleted successfully");

	//echo "Deleted successfully";
	}
	else
		  	 header("Location:http://localhost/stms/pages/courseadmin.php?err=SubjectCode is not there");

	//echo "Regno is not there";
	}
	if(isset($_POST['Search']))
 {
	header("Location:http://localhost/stms/pages/courseadmin.php?err1=$SubjectCode");
 }
 if(isset($_POST['Search2']))
 {
  $se=$_POST['Searcher'];
	header("Location:http://localhost/stms/pages/courseadmin.php?err2=$se");
 }
 
 
 
?>